﻿#pragma execution_character_set("utf-8")
#include "recordwidget.h"
#include "ui_recordwidget.h"
#include <QImage>
#include <QPixmap>
#include <QPainter>
#include "database.h"

RecordWidget::RecordWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecordWidget)
{
    ui->setupUi(this);
    m_state = true;
    QStringList header;
    header <<"时间"<<"商品编号"<<"名称"<<"数量"<<"种类"<<"成本"<<"售价";
    ui->tableWidget->setColumnCount(7);
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    ui->tableWidget->horizontalHeader()->setFixedHeight(50);

    ui->tableWidget->verticalHeader()->setHidden(true);
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(50);
    updateDisplay();
    connect(ui->tableWidget, SIGNAL(cellClicked(int,int)),this,SLOT(onHeaderClicked(int,int)));

}

RecordWidget::~RecordWidget()
{
    delete ui;
}

void RecordWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QImage t_image;
//    t_image.load(":/image/bg.png");
//    t_image = t_image.scaled(this->width(),this->height());
//    painter.drawPixmap(0,0,this->width(),this->height(),QPixmap::fromImage(t_image));

}
void RecordWidget::showEvent(QShowEvent *event)
{
    ui->tableWidget->horizontalHeader()->resizeSection(0,ui->tableWidget->width()*0.15);
    ui->tableWidget->horizontalHeader()->resizeSection(1,ui->tableWidget->width()*0.15);
    ui->tableWidget->horizontalHeader()->resizeSection(2,ui->tableWidget->width()*0.15);
    ui->tableWidget->horizontalHeader()->resizeSection(3,ui->tableWidget->width()*0.15);
    ui->tableWidget->horizontalHeader()->resizeSection(4,ui->tableWidget->width()*0.15);
    ui->tableWidget->horizontalHeader()->resizeSection(5,ui->tableWidget->width()*0.15);
    ui->tableWidget->horizontalHeader()->resizeSection(6,ui->tableWidget->width()*0.1);
    updateDisplay();
}


void RecordWidget::onHeaderClicked(int row,int column)
{
    selectrow = row;
    ui->lineEdit_a->setText(ui->tableWidget->item(row,0)->text());
    ui->lineEdit_b->setText(ui->tableWidget->item(row,1)->text());
    ui->lineEdit_c->setText(ui->tableWidget->item(row,2)->text());
    ui->lineEdit_d->setText(ui->tableWidget->item(row,3)->text());
    ui->lineEdit_e->setText(ui->tableWidget->item(row,4)->text());

    ui->lineEdit_f->setText(ui->tableWidget->item(row,5)->text());
    ui->lineEdit_g->setText(ui->tableWidget->item(row,6)->text());

}
void RecordWidget::updateDisplay()
{
    ui->tableWidget->clearContents();
    if (!DataBasePri::database.open()) {
        qCritical() << DataBasePri::database.lastError().text();
        return;
    }

    QSqlQuery query(DataBasePri::database);
    QString table = m_state ? "warehousing" : "delivery";
    QString sql = QString(
        "SELECT r.time, r.product_id, p.name, r.num, r.type, r.cost, r.price "
        "FROM %1 r "
        "JOIN products p ON r.product_id = p.id "
        "ORDER BY r.time DESC"
    ).arg(table);

    bool ret = query.exec(sql);
    if (!ret) {
        qCritical() << query.lastError().text();
        DataBasePri::database.close();
        return;
    }

    int row = 0;
    ui->tableWidget->setRowCount(0);
    while (query.next()) {
        ui->tableWidget->insertRow(row);
        for (int col = 0; col < 7; ++col) {
            ui->tableWidget->setItem(row, col, new QTableWidgetItem(query.value(col).toString()));
        }
        row++;
    }

    DataBasePri::database.close();
}


void RecordWidget::on_pushButton_1_clicked()
{
    // 清空详情区字段
    ui->lineEdit_a->clear();
    ui->lineEdit_c->clear();
    ui->lineEdit_d->clear();
    ui->lineEdit_e->clear();
    ui->lineEdit_f->clear();
    ui->lineEdit_g->clear();

    QString keyword = ui->lineEdit_b->text().trimmed();

    if (keyword.size() < 2) {
        updateDisplay();
        return;
    }

    ui->tableWidget->clearContents();

    if (!DataBasePri::database.open()) {
        qCritical() << DataBasePri::database.lastError().text();
        return;
    }

    QSqlQuery query(DataBasePri::database);
    QString table = m_state ? "warehousing" : "delivery";

    QString sql = QString(
        "SELECT r.time, r.product_id, p.name, r.num, r.type, r.cost, r.price "
        "FROM %1 r "
        "JOIN products p ON r.product_id = p.id "
        "WHERE p.name LIKE '%%2%' OR r.product_id LIKE '%%2%' "
        "ORDER BY r.time DESC"
    ).arg(table).arg(keyword);

    bool ret = query.exec(sql);
    if (!ret) {
        qCritical() << query.lastError().text();
        DataBasePri::database.close();
        return;
    }

    int row = 0;
    ui->tableWidget->setRowCount(0);
    while (query.next()) {
        ui->tableWidget->insertRow(row);
        for (int col = 0; col < 7; ++col) {
            ui->tableWidget->setItem(row, col, new QTableWidgetItem(query.value(col).toString()));
        }
        row++;
    }

    DataBasePri::database.close();
}


void RecordWidget::on_pushButton_2_clicked()
{
    emit sendDisWidget("m_SelectWidget");
}

void RecordWidget::on_radioButton_a_clicked(bool checked)
{
    ui->lineEdit_a->setText("");
    ui->lineEdit_c->setText("");
    ui->lineEdit_d->setText("");
    ui->lineEdit_e->setText("");
    ui->lineEdit_f->setText("");
    ui->lineEdit_g->setText("");
    m_state = true;
    updateDisplay();
}

void RecordWidget::on_radioButton_b_clicked()
{
    ui->lineEdit_a->setText("");
    ui->lineEdit_c->setText("");
    ui->lineEdit_d->setText("");
    ui->lineEdit_e->setText("");
    ui->lineEdit_f->setText("");
    ui->lineEdit_g->setText("");
    m_state = false;
    updateDisplay();
}




